#!/bin/bash
#zeze-ScriptCloud v2.0 by:zeze
#wechat:2585031577luhonglin  November 28, 2020
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
ip=`curl -l ip.sb`
siip=`ip addr | awk '/^[0-9]+: / {}; /inet.*global.*eth/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}'`
Menu() {
      cat <<EOF
       =================================================================
       ##############--------《zeze-ScriptCloud v2.0》---------##########
       #                                                        by:啧啧
       # 1)系统初始化
       # 2)安装docker-ce&配置镜像加速器&docker-compose&bash-complete
       # 3)一键部署gitlab<已修复无法安装maven的问题>(port:8088)
       # 4)一键部署jenkins(port:8080)
       # 5)一键部署k8s主(alpha)
       # 6)一键部署k8s从(alpha)
       # 7)一键部署jdk8u251环境(alpha)
       # 8）一键部署harbor(port:9999)
       # 8)安装常用辅助wget.unzip.vim.net-tools.....(alpha)
       # 9)查阅警告&帮助(alpha)
       # 10)升级脚本(alpha)
       # 11)退出
       #                                 warning:本脚本适用centos7.6-7.8
       #================================================================
EOF
}
#----------------------系统初始化--------————————————————————————————————————————————#
Systemctlenv() {
startTime=`date +%Y%m%d-%H:%M:%S`
startTime_s=`date +%s`
base=/etc/yum.repos.d/CentOS-Base.repo
if [ $base -e ]
  then
    echo "$base 存在，开始备份 "
    mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
    echo " 文件已备份至/etc/yum.repos.d/CentOS-Base.repo.backup "
elif [ cat /etc/yum.repos.d/CentOS-Base.repo  | grep "aliyuncs" ] ;then
    echo " 该源已经是aliyuncs "
else
    echo " 无法找到/etc/yum.repos.d/CentOS-Base.repo "
fi
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
echo "正在下载新的base源，请勿进行其他操作"
yum makecache
echo "正在进行生成缓存数据"
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
echo "开始安装扩展源"
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo $>/dev/null
echo "正在升级镜像，请勿操作～大致1-3分钟内升级完毕！"
yum update -y $>/dev/null
echo "恭喜～ yum源配置完毕！"
systemctl stop firewalld.service
systemctl disable firewalld.service
sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
echo " firewalld & selinux 均已关闭"
echo "开始执行安装axel"
yum -y install axel &> /dev/null
echo "axel安装完毕 "

endTime=`date +%Y%m%d-%H:%M:%S`
endTime_s=`date +%s`
sumTime=$[ $endTime_s - $startTime_s ]

echo "开始执行$startTime ---> 结束执行$endTime" "任务共计耗时:$sumTime s"

}
#----------------------安装docker-ce&配置镜像加速器&docker-compose&bash-complete--------————————————————#

docker_ce() {
startTime=`date +%Y%m%d-%H:%M:%S`
startTime_s=`date +%s`
rpm -q docker-ce
if [ $? = 0 ]
  then
  echo "------------------"else
  echo "docker 已安装或未卸载赶紧，请自检"
  continue
fi
echo "恭喜检测已经通过，正在火速安装docker中"        
yum install -y yum-utils device-mapper-persistent-data lvm2 &>/dev/null
开始安装docker依赖
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo &>/dev/null
echo "更新并且开始安装docker-ce" && sudo yum makecache fast &&sudo yum -y install docker-ce &>/dev/null
systemctl start docker
if [ $? =0 ]
then
  echo "docker 启动成功！"
  elif  ps -ef | grep docker |egrep -v "grep"
  then
  echo "docker 已经在运行了！"
  else
  echo "docker 启动失败！"
fi
systemctl enable docker
echo "docker开机自启成功！"
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://6z163kh1.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reloadi
echo "docker镜像加速配置完毕"
curl -L https://get.daocloud.io/docker/compose/releases/download/1.26.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose && cd /usr/local/bin/ && docker-compose --version
echo "docker-compose安装完毕"
yum install -y bash-completioni  &> /dev/null && source /usr/share/bash-completion/completions/docker &&  source /usr/share/bash-completion/bash_completionsource 
echo "恭喜 补全工具已经加载完毕"
endTime=`date +%Y%m%d-%H:%M:%S`
endTime_s=`date +%s`
sumTime=$[ $endTime_s - $startTime_s ]

echo "开始执行$startTime ---> 结束执行$endTime" "任务共计耗时:$sumTime s"

}
#---------------------------一键部署gitlab-----------------------------#
ilstallgitlab(){
startTime=`date +%Y%m%d-%H:%M:%S`
startTime_s=`date +%s`
mkdir -p /data/gitlab
cd /data/gitlab 
cat << EOF > /data/gitlab/docker-compose.yml
version: '3'
services:
  web:
    image: 'gitlab/gitlab-ce:latest'
    restart: always
    hostname: '$ip'
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'http://$ip'
        gitlab_rails['gitlab_shell_ssh_port'] = 2222
        unicorn['port'] = 8888
        nginx['listen_port'] = 80
    ports:
      - '8088:80'
      - '2222:22'
      - '2443:443'
    volumes:
      - '/data/gitlab/config:/etc/gitlab'
      - '/data/gitlab/logs:/var/log/gitlab'
      - '/data/gitlab/data:/var/opt/gitlab'
EOF
echo "---------准备部署gitlab-----------"
cd /data/gitlab/ && docker-compose up -d
echo "---------gitlab构建完毕-----------"
cd /data/gitlab/config/ && chmod 755 /data/gitlab/config/* && sed -i '66a\gitlab_rails['gitlab_ssh_host'] = '\'http://$ip/\''' /data/gitlab/config/gitlab.rb
gitlabpid=`docker ps  | grep gitlab_web_1 |awk '{print$1}'`
docker restart $gitlabpid
echo "gitlab_ssh_host 已经修改为http://$ip/，如果没有公网IP请自行修改/data/gitlab/config/gitlab.rb"
echo"访问方式：
阁下的公网IP: $ip:8088 
阁下的私网IP: $siip:8088
如果阁下无法访问，请查看帮助，或者使用docker logs gitlab,查看异常原因，或者联系我的战略合作伙伴*百度* 
映射文件目录
- '/data/gitlab/config:/etc/gitlab'
- '/data/gitlab/logs:/var/log/gitlab'
- '/data/gitlab/data:/var/opt/gitlab'"
endTime=`date +%Y%m%d-%H:%M:%S`
endTime_s=`date +%s`
sumTime=$[ $endTime_s - $startTime_s ]

echo "开始执行$startTime ---> 结束执行$endTime" "任务共计耗时:$sumTime s"
}
#---------------------------一键部署jenkins-----------------------------#
installjenkins(){
startTime=`date +%Y%m%d-%H:%M:%S`
startTime_s=`date +%s`
mkdir -p /data/jenkins
cd /data/jenkins
cat << EOF > /data/jenkins/docker-compose.yml
version: '3.1'
services:
 jenkins:
  image: jenkins/jenkins:lts
  volumes:
   - /data/jenkins/:/var/jenkins_home
   - /var/run/docker.sock:/var/run/docker.sock
   - /usr/bin/docker:/usr/bin/docker
   - /usr/lib/x86_64-linux-gnu/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7
   - /usr/local/maven:/var/maven_home
  ports:
   - "8080:8080"
  expose:
   - "8080"
   - "50000"
  privileged: true
  user: root
  restart: always
  container_name: jenkins
  environment:
   JAVA_OPTS: '-Djava.util.logging.config.file=/var/jenkins_home/log.properties'
EOF
######################这块的逻辑没想好#######################

# echo “开始预加载maven安装包”
# wget -P /data/jenkins/maven/ https://cdn.guituizhi.com/apache-maven-3.6.1-bin.tar.gz
# yum install -y java-1.8.0-openjdk  &>/dev/null
# cd /data/jenkins/maven/  && tar -zxvf apache-maven-3.6.1-bin.tar.gz && rm -rf /usr/local/maven && mv apache-maven-3.6.1 /usr/local/maven
# tee  -a /etc/profile <<-'EOF'
# MAVEN_HOME=/usr/local/maven
# export PATH=${MAVEN_HOME}/bin:$PATH
# EOF

# source /etc/profile  
# mvn -v
# if [ $? =0 ]
# then
#   echo "maven加载成功"
#   else
#   echo "maven加载失败"
# fi
# #<mirror>
# #      <id>mirrorId</id>153
# #      <mirrorOf>repositoryId</mirrorOf>154
# #      <name>Human Readable Name for this Mirror.</name>155
# #      <url>http://my.repository.com/repo/path</url>156
# #    </mirror>157
# sed -i '153{s/<id>mirrorId</id>/<id>aliyunmaven</id>}' /usr/local/maven/conf/settings.xml
# sed -i '154{s/<mirrorOf>repositoryId</mirrorOf>/<mirrorOf>*</mirrorOf>}' /usr/local/maven/conf/settings.xml
# sed -i '155{s/<name>Human Readable Name for this Mirror.</name>/<name>阿里云公共仓库</name>}' /usr/local/maven/conf/settings.xml
# sed -i '156{s/<url>http://my.repository.com/repo/path</url>/<url>https://maven.aliyun.com/repository/public</url>}' /usr/local/maven/conf/settings.xml

# sed -i '153,156 s/<id>mirrorId</id>
#       <mirrorOf>repositoryId</mirrorOf>
#       <name>Human Readable Name for this Mirror.</name>
#       <url>http://my.repository.com/repo/path</url>
#     </mirror>/<id>aliyunmaven</id>
#   <mirrorOf>*</mirrorOf>
#   <name>阿里云公共仓库</name>
#   <url>https://maven.aliyun.com/repository/public</url>
# </mirror>' /usr/local/maven/conf/settings.xml
####################想好了在写吧##################################
echo "---------准备部署jenkins-----------"
cd /data/jenkins/ && docker-compose up -d
echo "---------jenkins构建完毕-----------"
echo"访问方式：
阁下的公网IP: $ip:8080
阁下的私网IP: $siip:8080
如果阁下无法访问，请查看帮助，或者使用docker logs jenkins,查看异常原因，或者联系我的战略合作伙伴*百度* 
映射文件目录
   - /var/run/docker.sock:/var/run/docker.sock
   - /usr/bin/docker:/usr/bin/docker
   - /usr/lib/x86_64-linux-gnu/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7 "
endTime=`date +%Y%m%d-%H:%M:%S`
endTime_s=`date +%s`
sumTime=$[ $endTime_s - $startTime_s ]

echo "开始执行$startTime ---> 结束执行$endTime" "任务共计耗时:$sumTime s"
}



#---------------------------一键部署harbor-----------------------------#
installharbor(){
startTime=`date +%Y%m%d-%H:%M:%S`
startTime_s=`date +%s`
mkdir -p /data/harbor
echo "开始为阁下加速下载harborv2.1.1 请耐心等待。"
cd /data/harbor  && wget https://cdn.guituizhi.com/harbor-offline-installer-v2.1.1.tgz
tar -zxvf harbor-offline-installer-v2.1.1.tgz
echo "已经解压完毕，开始为阁下配置基本文件信息"
cd /data/harbor/harbor &&  cp harbor.yml.tmpl  harbor.yml 
sed -i "s/hostname: reg.mydomain.com/hostname: $ip/g" harbor.yml
sed -i "s/port: 80/port: 9999/g" /data/harbor/harbor/harbor.yml
sed -i "s/https:/#https:/g" /data/harbor/harbor/harbor.yml
sed -i "s/port: 443/#port: 443/g" /data/harbor/harbor/harbor.yml
cd /data/harbor/harbor && ./prepare 
if [ $? -eq 0 ]
  then
  echo "恭喜阁下检测通过"
  else
  echo "请阁下手动 ./prepare 查看并解决报错信息"
fi
echo "开始为阁下加载harbor镜像中～请耐心等待，很快就好"
cd /data/harbor/harbor && ./install.sh

echo "恭喜阁下已经安装完毕，以下下是访问路径及其端口

======================harbor=====================

公网访问：$ip:9999
内网访问：请阁下修改vim /data/harbor/harbor.yml :hostname: $siip 
harbor默认账号：admin 
      passwd：Harbor12345
database_password:root123
                                             
                                             祝好
================================================="
endTime=`date +%Y%m%d-%H:%M:%S`
endTime_s=`date +%s`
sumTime=$[ $endTime_s - $startTime_s ]

echo "开始执行$startTime ---> 结束执行$endTime" "任务共计耗时:$sumTime s"

}

while :
do
        Menu
        read -p "请阁下输入选项 ： " action
        case "$action" in
        1)
        Systemctlenv
        sleep 5
        ;;
        2)
        docker_ce
        sleep 5
        ;;
        3)
        ilstallgitlab
        echo "请等待15-30ms然后在刷新页面，docker启动后无法立即访问到gitlab。如果你用的是神威太湖之光那当我没说好了。"
        sleep 15        
        ;;
        4)
        installjenkins
        echo "请等待15-30ms然后在刷新页面，docker启动后无法立即访问到jenkins。如果你用的是神威太湖之光那当我没说好了. "
        sleep 15
        ;;
        8)
        installharbor
        sleep 20
        ;;
        11)
        exit
        ;;
        *)
        clear        
        ;;
        esac
done


#---------------------------狗命要紧改日在更-----------------------------#
# /*
#  *  ┏┓   ┏┓
#  * ┏┛┻━━━┛┻┓
#  * ┃       ┃
#  * ┃ ━　　　┃
#  * ┃┳┛  ┗┳ ┃
#  * ┃       ┃
#  * ┃   ┻   ┃
#  * ┃       ┃
#  * ┗━┓   ┏━┛
#  *   ┃   ┃   神兽保佑
#  *   ┃   ┃   代码无BUG!
#  *   ┃   ┗━━━┓
#  *   ┃       ┣┓
#  *   ┃       ┏┛
#  *   ┗┓┓┏━┳┓┏┛
#  *    ┃┫┫ ┃┫┫
#  *    ┗┻┛ ┗┻┛
#  */
#12月2日：增加了一键部署Harbor的功能，但是没有加入是否公网还是私网的判定。原因：懒
#目前已知BUG： axel多线程下载器在使用yum安装后由于版本较低，使用该软件下载时会出现重定向bug导致无法正常使用
#，故脚本中使用的都是单线程wget下载方式。后面等有时间了会使用源码编译的方式选择一个合适的版本做自动化安装的，请阁下放心。
#12月3日：发现了《2)》 功能模块中 bash-complete dockerz自动补全命令安装失效，状态 ：待修复，目前不打算修复，这个月的重心都安排在k8s的集群初始化的选择方案中，故先不做安排